﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UniversityPortal.Models;

using DataModel.Managers;
using DataModel.Models;

namespace UniversityPortal.Controllers
{
    public class RoleController : Controller
    {
        //
        // GET: /Role/
        [UAuthorize(UserRoles = "Admin")]
        public ActionResult Assume()
        {
            return View();
        }

        public ActionResult GetGroups()
        {
            GroupManager gm = new GroupManager();
            IEnumerable<SelectListItem> groups = gm.GetAllGroups().Select(g => new SelectListItem()
            {
                Text = g.Number.ToString(), 
                Value = g.Id.ToString()
            });
            return PartialView(groups);
        }

        public ActionResult GetUsers()
        {
            TypeOfUser userType;
            Guid id = Guid.Empty;
            GroupManager gm = new GroupManager();
            UserManager umanager = new UserManager();
            List<UserModel> listOfUserModels = umanager.GetAllUsers();

            IEnumerable<SelectListItem> usersList = null;
            object value = Request.QueryString[0] as object;
            if (!Enum.TryParse(value.ToString(), out userType))
            {
                id = Guid.Parse(value.ToString());
                usersList = gm.GetAllStudentsByGroup(id).Select(st => new SelectListItem() { Text = st.ToString(), Value = st.ID.ToString() });
            }
            else
            {
                usersList = listOfUserModels.Where(um => um.Type == userType).Select(u => new SelectListItem() { Text = u.ToString(), Value = u.ID.ToString() });
            }

            ViewBag.UserList = usersList;
            return PartialView("GetUsers");
        }

        public ActionResult Information()
        {
            UserManager um = new UserManager();
            List<RoleModel> roles = new RoleManager().GetAllRoles();
            ViewBag.Roles = roles.Select(r => new SelectListItem() { Text = r.Name, Value = r.ID.ToString() });
            return PartialView(um.GetUser(Guid.Parse(Request.QueryString[0])));
        }

        [HttpPost]
        [UAuthorize(UserRoles = "Admin")]
        public ActionResult AddRole()
        {
            object user = Request.Form["UID"];
            object role = Request.Form["Role"];
            Guid userID = Guid.Parse(user.ToString());
            Guid roleID = Guid.Parse(role.ToString());
            UserManager um = new UserManager();
            um.AddRoleToUser(userID, roleID);
            return RedirectToAction("Assume");
        }

        [HttpPost]
        [UAuthorize(UserRoles = "Admin")]
        public ActionResult DeleteRole()
        {
            object user = Request.Form["UID"];
            object role = Request.Form["Role"];
            Guid userID = Guid.Parse(user.ToString());
            Guid roleID = Guid.Parse(role.ToString());
            UserManager um = new UserManager();
            um.DeleteUserRole(userID, roleID);
            return RedirectToAction("Assume");
        }
    }
}
